package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.contract.mng.SolutionAddReq;
import com.homedo.odin.solution.contract.mng.SolutionDelReq;
import com.homedo.odin.solution.contract.mng.SolutionDetailResp;
import com.homedo.odin.solution.contract.mng.SolutionListInfoResp;
import com.homedo.odin.solution.contract.mng.SolutionSkuDelReq;
import com.homedo.odin.solution.contract.mng.SolutionSkuImportReq;
import com.homedo.odin.solution.contract.mng.SolutionSkuResp;
import com.homedo.odin.solution.contract.mng.SolutionSortReq;
import com.homedo.odin.solution.contract.mng.SolutionStatusReq;
import com.homedo.odin.solution.service.bizservice.mng.MngSolutionsBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhaoxudong
 * @date: 2018/3/27 17:30
 * @Description:
 */
@Api(description = "方案", tags = {"Solution"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/solutions")
public class MngSolutionsController extends BaseController<MngSolutionsBizService> {

    @ApiOperation(value = "MNG_获取方案列表", notes = "MNG_获取方案列表", response = SolutionListInfoResp.class)
    @GetMapping
    public ResultData solutions(
            @ApiParam(value = "方案编号", required = false) @RequestParam(value = "solutionId", required = false) Long solutionId,
            @ApiParam(value = "方案名称", required = false) @RequestParam(value = "solutionName", required = false) String solutionName,
            @ApiParam(value = "方案类型(1:专家方案 2:产品系统方案 3:普通用户方案)", required = false) @RequestParam(value = "solutionType", required = false) Integer solutionType,
            @ApiParam(value = "是否允许下载 0:不允许 1:允许", required = false) @RequestParam(value = "isAdmitDownload", required = false) Integer isAdmitDownload,
            @ApiParam(value = "专家姓名", required = false) @RequestParam(value = "expertName", required = false) String expertName,
            @ApiParam(value = "品牌Id", required = false) @RequestParam(value = "brandId", required = false) Integer brandId,
            @ApiParam(value = "状态 0:已保存 10:待专家审核  11：后台审核 20:审核通过 21:审核驳回 30:上架中 31:下架中", required = false) @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "是否删除 0：否 1：是", required = false) @RequestParam(value = "isDel", required = false) Integer isDel,
            @ApiParam(value = "排序字段(business_code,page_view,download_count,sort,create_time,charges)", required = false) @RequestParam(value = "sortField", required = false) String sortField,
            @ApiParam(value = "排序类型 ASC DESC", required = false) @RequestParam(value = "sortType", required = false) String sortType,
            @ApiParam(value = "业务编号", required = false) @RequestParam(value = "businessCode", required = false) String businessCode,
            @ApiParam(value = "用户名称", required = false) @RequestParam(value = "userName", required = false) String userName,
            @ApiParam(value = "规范来源（1：后台上传 2：前台上传）", required = false) @RequestParam(value = "formType", required = false) Integer formType,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        SolutionListInfoResp resp = baseService.solutions(solutionId, solutionName, solutionType,
                isAdmitDownload, expertName, brandId, status, businessCode, userName, formType, sortField, sortType,
                isDel, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "MNG_获取方案详情", notes = "MNG_获取方案详情", response = SolutionDetailResp.class)
    @GetMapping("/{solutionId}")
    public ResultData solution(@ApiParam("solutionId") @PathVariable("solutionId") Long solutionId) {
        SolutionDetailResp resp = baseService.findSolution(solutionId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "MNG_新增方案", notes = "MNG_新增方案")
    @PostMapping
    public ResultData addSolution(@RequestBody SolutionAddReq request) {
        baseService.add(request);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_修改方案删除状态", notes = "MNG_修改方案删除状态")
    @PutMapping("/del")
    public ResultData delSolution(@RequestBody SolutionDelReq req) {
        baseService.del(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_修改排序", notes = "MNG_修改排序")
    @PutMapping("/sort")
    public ResultData updateSort(@RequestBody SolutionSortReq req) {
        baseService.updateSort(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_修改方案", notes = "MNG_修改方案")
    @PutMapping()
    public ResultData edit(@RequestBody SolutionAddReq request) {
        baseService.edit(request);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_修改方案审核状态、上下架", notes = "MNG_修改方案审核状态、上下架")
    @PutMapping("/status")
    public ResultData status(@RequestBody SolutionStatusReq req) {
        baseService.status(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_根据方案id查询sku列表", notes = "MNG_根据方案id查询sku列表", response = SolutionSkuResp.class)
    @GetMapping("/{solutionIds}/skus")
    public ResultData getSkus(@PathVariable("solutionIds") String solutionIds) {
        if (StringUtils.isBlank(solutionIds)) {
            throw new BizException(Meta.CODE_451.getCode(), "solutionIds不合规");
        }
        List<SolutionSkuResp> resp = baseService.getSkus(solutionIds);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "MNG_移除方案关联sku", notes = "MNG_移除方案关联sku")
    @DeleteMapping("/skus")
    public ResultData removeSkus(@RequestBody SolutionSkuDelReq req) {
        baseService.removeSkus(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "MNG_新增方案关联sku", notes = "MNG_新增方案关联sku")
    @PostMapping("/skus")
    public ResultData ImportSkus(@RequestBody SolutionSkuImportReq req) {
        baseService.importSkus(req);
        return ResultData.newInstance();
    }


}
